package com.example.woke;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.http.HttpMethods;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.PersistentCookieStore;
import com.loopj.android.http.RequestParams;
import com.woke.method.Md5jarss;
import com.woke.method.MyApp;
import com.zhongcai.online.R;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

public class JiebangcarActivity extends Activity implements OnClickListener, Callback {

	private TextView mT1, mT2, mT3, mT4, mT5, mT6, mT7, mT8, mT9, mT11;
	private Handler mHandler;
	private String aaaa = "";
	private RelativeLayout mT12;//�h���I
	private TextView mTtext1,mTtext3,mTtext2,mTtext4,mTtext5,mTtext6;
	private String psw1 = "";
	private String psw2 = "";
	private String psw3 = "";
	private String psw4 = "";
	private String psw5 = "";
	private String psw6 = "";
	private MyApp application;
	private String carno;
	private RelativeLayout mBar;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_jiebangcar);
		
		carno = getIntent().getStringExtra("carno");
		intview();
		
	}

	private void intview() {
		application = (MyApp) getApplication();
		mHandler = new Handler(JiebangcarActivity.this);
		mBar = (RelativeLayout) findViewById(R.id.avjiebangc_progress);
		
		mT1 = (TextView) findViewById(R.id.jiebang_edit_1);
		mT2 = (TextView) findViewById(R.id.jiebang_edit_2);
		mT3 = (TextView) findViewById(R.id.jiebang_edit_3);
		mT4 = (TextView) findViewById(R.id.jiebang_edit_4);
		mT5 = (TextView) findViewById(R.id.jiebang_edit_5);
		mT6 = (TextView) findViewById(R.id.jiebang_edit_6);
		mT7 = (TextView) findViewById(R.id.jiebang_edit_7);
		mT8 = (TextView) findViewById(R.id.jiebang_edit_8);
		mT9 = (TextView) findViewById(R.id.jiebang_edit_9);
		mT11 = (TextView) findViewById(R.id.jiebang_edit_11);
		mT12 = (RelativeLayout) findViewById(R.id.jiebang_edit_12);
		
		mTtext1 = (TextView) findViewById(R.id.jiebang_text_1);
		mTtext2 = (TextView) findViewById(R.id.jiebang_text_2);
		mTtext3 = (TextView) findViewById(R.id.jiebang_text_3);
		mTtext4 = (TextView) findViewById(R.id.jiebang_text_4);
		mTtext5 = (TextView) findViewById(R.id.jiebang_text_5);
		mTtext6 = (TextView) findViewById(R.id.jiebang_text_6);
		
		mT1.setOnClickListener(this);
		mT2.setOnClickListener(this);
		mT3.setOnClickListener(this);
		mT4.setOnClickListener(this);
		mT5.setOnClickListener(this);
		mT6.setOnClickListener(this);
		mT7.setOnClickListener(this);
		mT8.setOnClickListener(this);
		mT9.setOnClickListener(this);
		mT11.setOnClickListener(this);
		mT12.setOnClickListener(this);
		
		
		findViewById(R.id.avjiebangc_image_back).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	@Override
	public void onClick(View v) {
	Message msg = mHandler.obtainMessage();
	switch (v.getId()) {
	case R.id.jiebang_edit_1:
		msg.what = 0x101;
		msg.arg1 = 1;
		mHandler.sendMessage(msg);
		break;

	case R.id.jiebang_edit_2:
		msg.what = 0x101;
		msg.arg1 = 2;
		mHandler.sendMessage(msg);
		break;
	case R.id.jiebang_edit_3:
		msg.what = 0x101;
		msg.arg1 = 3;
		mHandler.sendMessage(msg);
		break;
	case R.id.jiebang_edit_4:
		msg.what = 0x101;
		msg.arg1 = 4;
		mHandler.sendMessage(msg);
		break;
	case R.id.jiebang_edit_5:
		msg.what = 0x101;
		msg.arg1 = 5;
		mHandler.sendMessage(msg);
		break;
	case R.id.jiebang_edit_6:
		msg.what = 0x101;
		msg.arg1 = 6;
		mHandler.sendMessage(msg);
		break;
	case R.id.jiebang_edit_7:
		msg.what = 0x101;
		msg.arg1 = 7;
		mHandler.sendMessage(msg);
		break;
	case R.id.jiebang_edit_8:
		msg.what = 0x101;
		msg.arg1 = 8;
		mHandler.sendMessage(msg);
		break;
	case R.id.jiebang_edit_9:
		msg.what = 0x101;
		msg.arg1 = 9;
		mHandler.sendMessage(msg);
		break;
	case R.id.jiebang_edit_11:
		msg.what = 0x101;
		msg.arg1 = 0;
		mHandler.sendMessage(msg);
		break;
	case R.id.jiebang_edit_12:
		msg.what = 0x102;
		mHandler.sendMessage(msg);
		break;

	default:
		break;
	}
	}

	@Override
	public boolean handleMessage(Message msg) {
	if (msg.what == 0x101) {
		int arg1 = msg.arg1;
		aaaa = aaaa + arg1;
		aaaa = aaaa.trim();
			if(aaaa.length() == 1){
				psw1 = ""+arg1;
				mTtext1.setText(psw1);
			}else if(aaaa.length() == 2){
				psw2 = ""+arg1;
				mTtext2.setText(psw2);
			}else if(aaaa.length() == 3){
				psw3 = ""+arg1;
				mTtext3.setText(psw3);
			}else if(aaaa.length() == 4){
				psw4 = ""+arg1;
				mTtext4.setText(psw4);
			}else if(aaaa.length() == 5){
				psw5 = ""+arg1;
				mTtext5.setText(psw5);
			}else if(aaaa.length() == 6){
				psw6 = ""+arg1;
				mTtext6.setText(psw6);
			}
		String trim1 = mTtext1.getText().toString().trim();
		String trim2 = mTtext2.getText().toString().trim();
		String trim3 = mTtext3.getText().toString().trim();
		String trim4 = mTtext4.getText().toString().trim();
		String trim5 = mTtext5.getText().toString().trim();
		String trim6 = mTtext6.getText().toString().trim();
		if(!trim1.equals("") && !trim2.equals("") && !trim3.equals("") && !trim4.equals("")
				&&!trim5.equals("") && !trim6.equals("")){
			//�@�Y�⽉
			String get32md5 = Md5jarss.get32MD5("sp_");
			String get32md52 = Md5jarss.get32MD5(aaaa);
			String sp1 = get32md5.substring(0, 12);
			String sp11 = get32md5.substring(get32md5.length()-4, get32md5.length());
			String pswold = sp1 +get32md52+sp11;
			if(application.getDatas_load ()!=null&&!pswold.equals(application.getDatas_load().getPay_pwd())){
				Log.e("application.getDatas_",application.getDatas_load().getPay_pwd());
				Toast.makeText(JiebangcarActivity.this, "支付密码不正确", Toast.LENGTH_SHORT).show();

//				finish();
			}else{
				getbanklist();
			}
			aaaa = trim1+trim2+trim3+trim4+trim5+trim6;
		}
		
	}else{
		if(!aaaa.equals("")){
			aaaa = aaaa.trim();
			if(aaaa.length() == 1){
				aaaa = "";
				mTtext1.setText(aaaa);
			}else if(aaaa.length() == 2){
				aaaa = aaaa.substring(0,1);
				mTtext2.setText("");
			}else if(aaaa.length() == 3){
				aaaa = aaaa.substring(0,2);
				mTtext3.setText("");
			}else if(aaaa.length() == 4){
				aaaa = aaaa.substring(0,3);
				mTtext4.setText("");
			}else if(aaaa.length() == 5){
				aaaa = aaaa.substring(0,4);
				mTtext5.setText("");
			}else if(aaaa.length() == 6){
				aaaa = aaaa.substring(0,5);
				mTtext6.setText("");
			}
		}else{
			
		}
	}
	return false;
}
	
	private void getbanklist() {
		if(application.getDatas_load ()==null)return;
		AsyncHttpClient ahc = new AsyncHttpClient();
		PersistentCookieStore myCookieStore = new PersistentCookieStore(
				JiebangcarActivity.this);
		ahc.setCookieStore(myCookieStore);
		RequestParams params = new RequestParams();
		params.put("g", "api");
		params.put("m", "bankList");
		params.put("a", "unBind");
		params.put("user_id", application.getDatas_load().getId());
		params.put("cardno", carno);
		Log.e("getbanklist","user_id"+application.getDatas_load().getId()+"carno"+carno);
		ahc.get(HttpMethods.BASEURL, params, new JsonHttpResponseHandler() {
			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				super.onSuccess(statusCode, headers, response);
				try {
					String flag = response.getString("flag");
					String info = response.getString("info");
					 if (flag.equals("success")) {
						 Toast.makeText(JiebangcarActivity.this, ""+info,
									Toast.LENGTH_SHORT).show(); 
						 setResult(11);
						 finish();
				}else{
					 Toast.makeText(JiebangcarActivity.this, info,
								Toast.LENGTH_SHORT).show(); 
				}
				}catch (JSONException e) {
					e.printStackTrace();
				}
			}

			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {
				super.onFailure(statusCode, headers, responseString, throwable);
				Toast.makeText(JiebangcarActivity.this, "解綁失败！",
						Toast.LENGTH_SHORT).show();
				finish();
			}

			@Override
			public void onStart() {
				super.onStart();
				mBar.setVisibility(View.VISIBLE);
			}

			@Override
			public void onFinish() {
				super.onFinish();
				mBar.setVisibility(View.GONE);
			}
		});
	}
	
}
